package com.mrd.frame.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.frame.domain.AccessConfig;
import com.mrd.frame.domain.WhiteListConfig;
import com.mrd.frame.domain.message.CommonMessage;
import com.mrd.frame.domain.params.BaseDto;
import com.mrd.frame.remote.Validate;
import com.mrd.frame.service.WhiteListConfigService;

@Controller
public class WhiteListConfigController {
         
     private static final Log logger = LogFactory
			.getLog(WhiteListConfigController.class);
 	@Autowired
 	private Validate validate; 
    @Autowired
 	private WhiteListConfigService whiteListConfigService;
	
    @RequestMapping(value = "/whiteListConfig", method = RequestMethod.GET)
	public String whiteListConfig(){
		return "/config/WhiteListConfig";
	}
    
    @ResponseBody
	@RequestMapping("/whiteListConfigSaveOrUpdate")
	public CommonMessage saveOrUpdate(@ModelAttribute WhiteListConfig whiteListConfig) {
		CommonMessage msg = new CommonMessage();
		String validateResult = validate.checkParams(whiteListConfig);
		if(validateResult != null){
			msg.setCode(-1);
			msg.setMessage(validateResult);
			return msg;
		}
		try {
			if (StringUtils.isNotEmpty(whiteListConfig.getWhiteListConfigId())) {// 更新操作
				whiteListConfigService.updateByPrimaryKeySelective(whiteListConfig);
				msg.setMessage("修改成功");
			} else {// 新增
				int i = whiteListConfigService.insertSelective(whiteListConfig);
				if(i >= 0){
					msg.setMessage("保存成功");
				}
			}
		} catch (Exception e) {
			msg.setCode(-1);
			msg.setMessage(e.getMessage());
			logger.error(e);
			e.printStackTrace();
		}
		return msg;
	}
    
    @ResponseBody
    @RequestMapping("/whiteListConfigDelete")
    public CommonMessage whiteListConfigDelete(String ids) {
    	CommonMessage msg = new CommonMessage();
    	try {
    		whiteListConfigService.deleteByPrimaryKeys(ids);
    		msg.setCode(0);
    		msg.setMessage("删除成功");
    	} catch (Exception e) {
    		msg.setCode(-1);
    		msg.setMessage(e.getMessage());
    		logger.error(e);
    		e.printStackTrace();
    	}
    	return msg;
    }
    
    @ResponseBody
    @RequestMapping("/whiteListConfigGetById")
    public BaseDto whiteListConfigGetById(String id) {
    	 WhiteListConfig  whiteListConfig = whiteListConfigService.selectByPrimaryKey(id);
    	BaseDto dto = new BaseDto();
    	dto.setObjDetail(whiteListConfig);
    	return dto;
    }
    
    @ResponseBody
    @RequestMapping("/whiteListConfigGetPageList")
    public BaseDto whiteListConfigGetPageList(@ModelAttribute WhiteListConfig whiteListConfig, @ModelAttribute BaseDto bdt) {
    	if(StringUtils.isEmpty(whiteListConfig.getUserKey())){
    		whiteListConfig.setUserKey(null);
    	}
    	if(StringUtils.isEmpty(whiteListConfig.getIp())){
    		whiteListConfig.setIp(null);
    	}
    	PageHelper.startPage(bdt.page,bdt.pageSize);
    	List<WhiteListConfig> whiteListConfigList = whiteListConfigService.selectSelective(whiteListConfig);
    	PageInfo<WhiteListConfig> pi = new PageInfo<WhiteListConfig>(whiteListConfigList);
    	bdt.setTotal((int) pi.getTotal());
    	bdt.setRows(whiteListConfigList);
    	return bdt;
    }
}